﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NetworkInterfaceUtility;

namespace CluelessGame
{
    /// <summary>
    /// Interaction logic for CharacterSelectionScreen.xaml
    /// </summary>
    public partial class CharacterSelectionScreen : Window
    {
        public GameManager CurrentGameManager;

        public CharacterSelectionScreen(GameManager gameManager)
        {
            CurrentGameManager = gameManager;

            InitializeComponent();

            RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.Fant);

            if (!App.NIU.Hosting)
            {
                buttonStart.Visibility = System.Windows.Visibility.Collapsed;
            }
            App.NIU.RegisterNetworkMessageHandler((int)NIUMessageIDs.CHARACTER_SELECTED, HandleCharacterSelectedNetworkMessage, Dispatcher.BeginInvoke);
        }

        private ToggleButton _localSelection = null;
        private bool _forceClose = false;

        public Character SelectedCharacter
        {
            get { return  GetCharacterFromButton(_localSelection); }
        }

        public void ForceClose()
        {
            _forceClose = true;
            Close();
        }

        private void HandleCharacterSelectedNetworkMessage(NetworkMessageData message)
        {
            CharacterSelectedMessage characterSelectedMessage = message as CharacterSelectedMessage;

            if (characterSelectedMessage.character != characterSelectedMessage.prevCharacter)
            {
                CurrentGameManager.Players[characterSelectedMessage.SourceClientID + 1].AssociatedCharacter = characterSelectedMessage.character;
            }
            else
            {
                CurrentGameManager.Players[characterSelectedMessage.SourceClientID + 1].AssociatedCharacter = null;
            }

            ToggleButton selectedButton = GetButtonFromCharacter(characterSelectedMessage.character);
            ToggleButton prevSelectedButton = GetButtonFromCharacter(characterSelectedMessage.prevCharacter);

            if (selectedButton != null)
            {
                selectedButton.IsEnabled = false;
                if (selectedButton == _localSelection)
                {
                    selectedButton.IsChecked = false;
                    _localSelection = null;
                }
            }
            if (prevSelectedButton != null)
            {
                prevSelectedButton.IsEnabled = true;
            }
        }

        private ToggleButton GetButtonFromCharacter(Character character)
        {
            if (character == GameManager.CurrentGameObjects.ProfessorPlum)
            {
                return toggleButtonPlum;
            }
            if (character == GameManager.CurrentGameObjects.MrGreen)
            {
                return toggleButtonGreen;
            }
            if (character == GameManager.CurrentGameObjects.MissScarlet)
            {
                return toggleButtonScarlet;
            }
            if (character == GameManager.CurrentGameObjects.MsPeakcock)
            {
                return toggleButtonPeacock;
            }
            if (character == GameManager.CurrentGameObjects.MsWhite)
            {
                return toggleButtonWhite;
            }
            if (character == GameManager.CurrentGameObjects.ColonelMustard)
            {
                return toggleButtonMustard;
            }
            return null;
        }

        private Character GetCharacterFromButton(ToggleButton button)
        {
            if (button == toggleButtonPlum)
            {
                return GameManager.CurrentGameObjects.ProfessorPlum;
            }
            if (button == toggleButtonGreen)
            {
                return GameManager.CurrentGameObjects.MrGreen;
            }
            if (button == toggleButtonScarlet)
            {
                return GameManager.CurrentGameObjects.MissScarlet;
            }
            if (button == toggleButtonPeacock)
            {
                return GameManager.CurrentGameObjects.MsPeakcock;
            }
            if (button == toggleButtonWhite)
            {
                return GameManager.CurrentGameObjects.MsWhite;
            }
            if (button == toggleButtonMustard)
            {
                return GameManager.CurrentGameObjects.ColonelMustard;
            }
            return null;
        }

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection == null)
            {
                MessageBox.Show("You must select a character!");
                return;
            }

            foreach (Player player in CurrentGameManager.Players)
            {
                if (player.InGame && player.AssociatedCharacter == null)
                {
                    MessageBox.Show("All players must select a character!");
                    return;
                }
            }

            int playerTurn = App.Rand.Next() % GameManager.CurrentGameObjects.ActivePlayerCount;
            App.NIU.BroadcastNetworkMessage(new StartGameMessage((int)NIUMessageIDs.START_GAME, playerTurn));

            CurrentGameManager.StartGame(playerTurn);

            ForceClose();

        }

        private void toggleButtonPlum_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection != null && _localSelection != toggleButtonPlum)
            {
                _localSelection.IsChecked = false;
            }

            App.NIU.BroadcastNetworkMessage(
                new CharacterSelectedMessage(
                    (int)NIUMessageIDs.CHARACTER_SELECTED, 
                    GetCharacterFromButton(toggleButtonPlum), 
                    GetCharacterFromButton(_localSelection)));

            if (_localSelection != toggleButtonPlum)
            {
                _localSelection = toggleButtonPlum;
                CurrentGameManager.LocalPlayer.AssociatedCharacter = SelectedCharacter;
            }
            else
            {
                _localSelection = null;
            }
        }

        private void toggleButtonMustard_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection != null && _localSelection != toggleButtonMustard)
            {
                _localSelection.IsChecked = false;
            }

            App.NIU.BroadcastNetworkMessage(
                new CharacterSelectedMessage(
                    (int)NIUMessageIDs.CHARACTER_SELECTED,
                    GetCharacterFromButton(toggleButtonMustard),
                    GetCharacterFromButton(_localSelection)));

            if (_localSelection != toggleButtonMustard)
            {
                _localSelection = toggleButtonMustard;
                CurrentGameManager.LocalPlayer.AssociatedCharacter = SelectedCharacter;
            }
            else
            {
                _localSelection = null;
            }
        }

        private void toggleButtonScarlet_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection != null && _localSelection != toggleButtonScarlet)
            {
                _localSelection.IsChecked = false;
            }

            App.NIU.BroadcastNetworkMessage(
                new CharacterSelectedMessage(
                    (int)NIUMessageIDs.CHARACTER_SELECTED,
                    GetCharacterFromButton(toggleButtonScarlet),
                    GetCharacterFromButton(_localSelection)));

            if (_localSelection != toggleButtonScarlet)
            {
                _localSelection = toggleButtonScarlet;
                CurrentGameManager.LocalPlayer.AssociatedCharacter = SelectedCharacter;
            }
            else
            {
                _localSelection = null;
            }
        }

        private void toggleButtonGreen_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection != null && _localSelection != toggleButtonGreen)
            {
                _localSelection.IsChecked = false;
            }

            App.NIU.BroadcastNetworkMessage(
                new CharacterSelectedMessage(
                    (int)NIUMessageIDs.CHARACTER_SELECTED,
                    GetCharacterFromButton(toggleButtonGreen),
                    GetCharacterFromButton(_localSelection)));

            if (_localSelection != toggleButtonGreen)
            {
                _localSelection = toggleButtonGreen;
                CurrentGameManager.LocalPlayer.AssociatedCharacter = SelectedCharacter;
            }
            else
            {
                _localSelection = null;
            }
        }

        private void toggleButtonPeacock_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection != null && _localSelection != toggleButtonPeacock)
            {
                _localSelection.IsChecked = false;
            }

            App.NIU.BroadcastNetworkMessage(
                new CharacterSelectedMessage(
                    (int)NIUMessageIDs.CHARACTER_SELECTED,
                    GetCharacterFromButton(toggleButtonPeacock),
                    GetCharacterFromButton(_localSelection)));


            if (_localSelection != toggleButtonPeacock)
            {
                _localSelection = toggleButtonPeacock;
                CurrentGameManager.LocalPlayer.AssociatedCharacter = SelectedCharacter;
            }
            else
            {
                _localSelection = null;
            }
        }

        private void toggleButtonWhite_Click(object sender, RoutedEventArgs e)
        {
            if (_localSelection != null && _localSelection != toggleButtonWhite)
            {
                _localSelection.IsChecked = false;
            }

            App.NIU.BroadcastNetworkMessage(
                new CharacterSelectedMessage(
                    (int)NIUMessageIDs.CHARACTER_SELECTED,
                    GetCharacterFromButton(toggleButtonWhite),
                    GetCharacterFromButton(_localSelection)));

            if (_localSelection != toggleButtonWhite)
            {
                _localSelection = toggleButtonWhite;
                CurrentGameManager.LocalPlayer.AssociatedCharacter = SelectedCharacter;
            }
            else
            {
                _localSelection = null;
            }
        }

        private void Window_Closed_1(object sender, EventArgs e)
        {
            App.NIU.UnregisterNetworMessageHandler((int)NIUMessageIDs.CHARACTER_SELECTED);
        }

        private void CharacterSelectionScreen_Closing_1(object sender, CancelEventArgs e)
        {
            if (!_forceClose)
            {
                e.Cancel = true;
            }
        }
    }
}
